<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js misc - sound</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				font-weight: bold;
				text-align:center;
			}

			a {
				color:#0078ff;
			}

			#info {
				color:#fff;
				position: absolute;
				top: 0px; left: 0px; width: 50%;
				padding: 5px;
				z-index:100;
			}
		</style>
	</head>
	<body>

		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a> - webgl 3d sounds example -
			music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank">larrylarrybb</a> and
			<a href="http://www.newgrounds.com/audio/listen/376737" target="_blank">skullbeatz</a>  and
			<a href="http://opengameart.org/content/project-utopia-seamless-loop" target="_blank">congusbongus</a><br/><br/>
			navigate with WASD / arrows / mouse
		</div>


		<div id="container"></div>

		<script src="../build/three.js"></script>

		<script src="js/controls/FirstPersonControls.js"></script>

		<script src="js/Detector.js"></script>

		<script src="js/libs/dat.gui.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container;
			var camera, controls, scene, renderer;
			var light, pointLight;

			var mesh;
			var material_sphere1, material_sphere2;

			var analyser1, analyser2, analyser3;

			var clock = new THREE.Clock();

			init();
			animate();

			function init() {

				container = document.getElementById( 'container' );

				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
				camera.position.set( 0, 25, 0 );

				var listener = new THREE.AudioListener();
				camera.add( listener );

				scene = new THREE.Scene();
				scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );

				light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 0, 0.5, 1 ).normalize();
				scene.add( light );

				var sphere = new THREE.SphereGeometry( 20, 32, 16 );

				material_sphere1 = new THREE.MeshPhongMaterial( { color: 0xffaa00, shading: THREE.FlatShading, shininess: 0 } );
				material_sphere2 = new THREE.MeshPhongMaterial( { color: 0xff2200, shading: THREE.FlatShading, shininess: 0 } );
				material_sphere3 = new THREE.MeshPhongMaterial( { color: 0x6622aa, shading: THREE.FlatShading, shininess: 0 } );

				// sound spheres

				var audioLoader = new THREE.AudioLoader();

				var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
				mesh1.position.set( -250, 30, 0 );
				scene.add( mesh1 );

				var sound1 = new THREE.PositionalAudio( listener );
				audioLoader.load( 'sounds/358232_j_s_song.ogg', function( buffer ) {
					sound1.setBuffer( buffer );
					sound1.setRefDistance( 20 );
					sound1.play();
				});
				mesh1.add( sound1 );

				//

				var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
				mesh2.position.set( 250, 30, 0 );
				scene.add( mesh2 );

				var sound2 = new THREE.PositionalAudio( listener );
				audioLoader.load( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg', function( buffer ) {
					sound2.setBuffer( buffer );
					sound2.setRefDistance( 20 );
					sound2.play();
				});
				mesh2.add( sound2 );

				//


				var mesh3 = new THREE.Mesh( sphere, material_sphere3 );
				mesh3.position.set( 0, 30, -250 );
				scene.add( mesh3 );

				var sound3 = new THREE.PositionalAudio( listener );
				var oscillator = listener.context.createOscillator();
				oscillator.type = 'sine';
				oscillator.frequency.value = 144;
				oscillator.start(0);
				sound3.setNodeSource(oscillator);
				sound3.setRefDistance( 20 );
				sound3.setVolume(0.5);
				mesh3.add(sound3);

				// analysers

				analyser1 = new THREE.AudioAnalyser( sound1, 32 );
				analyser2 = new THREE.AudioAnalyser( sound2, 32 );
				analyser3 = new THREE.AudioAnalyser( sound3, 32 );

				// global ambient audio

				var sound4 = new THREE.Audio( listener );
				audioLoader.load( 'sounds/Project_Utopia.ogg', function( buffer ) {
					sound4.setBuffer( buffer );
					sound4.setLoop(true);
					sound4.setVolume(0.5);
					sound4.play();
				});

				// ground

				var helper = new THREE.GridHelper( 1000, 10, 0x444444, 0x444444 );
				helper.position.y = 0.1;
				scene.add( helper );

				//
    			var SoundControls = function() {
					   this.master = listener.getMasterVolume();
					   this.firstSphere =  sound1.getVolume();
					   this.secondSphere =  sound2.getVolume();
					   this.thirdSphere = sound3.getVolume();
					   this.Ambient =  sound4.getVolume();
				};
				var GeneratorControls = function() {
					   this.frequency = oscillator.frequency.value;
					   this.wavetype = oscillator.type;
				};
      			var gui = new dat.GUI();
				var soundControls = new SoundControls();
				var generatorControls = new GeneratorControls();
				var volumeFolder = gui.addFolder('sound volume');
				var generatorFolder = gui.addFolder('sound generator');
				volumeFolder.add(soundControls, 'master').min(0.0).max(1.0).step(0.01).onChange(function() {
					listener.setMasterVolume(soundControls.master);
				});
				volumeFolder.add(soundControls, 'firstSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
					sound1.setVolume(soundControls.firstSphere);
				});
				volumeFolder.add(soundControls, 'secondSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
					sound2.setVolume(soundControls.secondSphere);
				});

				volumeFolder.add(soundControls, 'thirdSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
					sound3.setVolume(soundControls.thirdSphere);
				});
				volumeFolder.add(soundControls, 'Ambient').min(0.0).max(1.0).step(0.01).onChange(function() {
					sound4.setVolume(soundControls.Ambient);
				});
				volumeFolder.open();
				generatorFolder.add(generatorControls, 'frequency').min(50.0).max(5000.0).step(1.0).onChange(function() {
					oscillator.frequency.value = generatorControls.frequency;
				});
				generatorFolder.add(generatorControls, 'wavetype', ['sine', 'square', 'sawtooth', 'triangle']).onChange(function() {
					oscillator.type = generatorControls.wavetype;
				});
				generatorFolder.open();




				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );

				container.innerHTML = "";
				container.appendChild( renderer.domElement );

				//
				controls = new THREE.FirstPersonControls( camera, renderer.domElement );

				controls.movementSpeed = 70;
				controls.lookSpeed = 0.05;
				controls.noFly = true;
				controls.lookVertical = false;



				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

				controls.handleResize();

			}

			function animate() {

				requestAnimationFrame( animate );
				render();

			}


			function render() {

				var delta = clock.getDelta();

				controls.update( delta );

				material_sphere1.emissive.b = analyser1.getAverageFrequency() / 256;
				material_sphere2.emissive.b = analyser2.getAverageFrequency() / 256;
				material_sphere3.emissive.b = analyser3.getAverageFrequency() / 256;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
